package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.File;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class FileDao {
	private DBHelp<File> db = new DBHelp<File>();
	
	public void save(File file){
		String sql = "INSERT INTO t_file (id,filename,filesize,createtime,fileTypeId,userId,'name')VALUES(?,?,?,?,?,?,?)";
		db.executeSQL(sql, file.getId(),file.getFileName(),file.getFileSize(),file.getCreatetime(),file.getFileTypeId(),file.getUserId(),file.getName());
	}
	
	public void update(File file){
		String sql  = "update t_file set fileName=?fileSize=?createtime=?fileTypeId=?userId=?,name=?where id=?";
		db.executeSQL(sql, file.getName(),file.getFileSize(),file.getCreatetime(),file.getFileTypeId(),file.getUserId(),file.getName(),file.getId());
	}
	public void del(File file){
		String sql = "delete from t_file where id=?";
		db.executeSQL(sql, "id");
	}
	public List<File> findByFileType(File file){
		String sql = "select id,fileName,fileSize,createtime,userId,name from t_file where fileTypeId=?";
		return db.executeQueryToList(sql, new FileRowMapper(), "fileTypeId");
	}
	
	private class FileRowMapper implements RowMapper<File>{

		public File mapRow(ResultSet rs) throws SQLException {
			File file= new File();
			
			file.setId(rs.getInt("id"));
			file.setFileName(rs.getString("fileName"));
			file.setFileSize(rs.getString("fileSize"));
			file.setCreatetime(rs.getString("createtime"));
			file.setFileTypeId(rs.getInt("fileTypeId"));
			file.setUserId(rs.getInt("userId"));
			file.setName(rs.getString("name"));
			return file;
		}
		
	}
}
